package com.minstone.common.bean;

import java.util.ArrayList;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 统一结果返回格式数据
 * */
public class ResultBean {
	/**
	 * 状态
	 * */
	private String status;
	/**
	 * 描述
	 * */
	private String desc;
	/**
	 * 数据返回
	 * */
	private Object data;
	
	/**
	 * 自定义返回信息
	 * 
	 * @param status
	 * @param desc
	 * @param data
	 * @return
	 */
    public static ResultBean build(String status, String desc, Object data) {
        return new ResultBean(status, desc, data);
    }

    /**
     * 处理成功
     * 
     * @param data
     * @return
     */
    public static ResultBean ok(Object data) {
        return new ResultBean(data);
    }

    /**
     * 处理失败
     * 
     * @param desc
     * @param data
     * @return
     */
    public static ResultBean fail(String desc,Object data) {
        return new ResultBean(ResultCode.BADREQUEST, desc, data);
    }
    
    public static ResultBean fail(String desc) {
        return new ResultBean(ResultCode.BADREQUEST, desc, null);
    }
    
    public static ResultBean ok() {
        return new ResultBean(null);
    }

    public ResultBean() {

    }

    public static ResultBean build(String status, String desc) {
        return new ResultBean(status, desc, null);
    }

    public ResultBean(String status, String desc, Object data) {
        this.status = status;
        this.desc = desc;
        this.data = data;
    }

    public ResultBean(Object data) {
        this.status = ResultCode.OK;
        this.desc = "处理成功";
        this.data = data;
    }
	

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String toJson() {
		JSONObject objs = new JSONObject();

		objs.put("desc", desc);
		objs.put("status", status);

		if (data instanceof ArrayList) {
			JSONArray array = new JSONArray();
			array = JSONArray.fromObject(data);
			objs.put("data", array);
		} else {

			objs.put("data", JSONObject.fromObject(data));
		}

		return objs.toString();

	}

}
